/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DefaultConversationHistoryWindow.java
 *
 * Created on 15-mar-2009, 9:30:41
 */

package client.ui.theme.defaultTheme;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import client.engine.ChatEngine;
import client.ui.IConversationHistoryWindow;

/**
 * Class DefaultConversationHistoryWindow
 * 
 * @author Tomas
 * @date March 15 2009
 */
public class DefaultConversationHistoryWindow extends javax.swing.JFrame implements IConversationHistoryWindow{

    private ChatEngine engine = null;
    private String path = null;

    public DefaultConversationHistoryWindow() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    private void initComponents() {

        filePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        fileList = new javax.swing.JList();
        detailsPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        detailsText = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Parlanchin - Browse conversation history");
        setResizable(false);

        filePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Conversations"));

        fileList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(fileList);

        javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
        filePanel.setLayout(filePanelLayout);
        filePanelLayout.setHorizontalGroup(
            filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addContainerGap())
        );
        filePanelLayout.setVerticalGroup(
            filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                .addContainerGap())
        );

        detailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Details"));

        detailsText.setColumns(20);
        detailsText.setEditable(false);
        detailsText.setLineWrap(true);
        detailsText.setRows(5);
        detailsText.setWrapStyleWord(true);
        jScrollPane2.setViewportView(detailsText);

        javax.swing.GroupLayout detailsPanelLayout = new javax.swing.GroupLayout(detailsPanel);
        detailsPanel.setLayout(detailsPanelLayout);
        detailsPanelLayout.setHorizontalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addContainerGap())
        );
        detailsPanelLayout.setVerticalGroup(
            detailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(detailsPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(detailsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(detailsPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(filePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }

    private void fileListMouseClicked(java.awt.event.MouseEvent evt) {
        
    	String file = (String) this.fileList.getSelectedValue();
    	
    	String filepath = path + file;
    	this.detailsText.setText("");
    	
    	try{
    		FileReader f = new FileReader(filepath);
    		BufferedReader buffer = new BufferedReader(f);
    		
    		while(true){
    			String line = buffer.readLine();
    			
    			if(line == null){
    				break;
    			}
    			
    			this.detailsText.append(line + "\n");
    		}
    	}catch(Exception e){
    		
    	}
    	
    }


    private javax.swing.JPanel detailsPanel;
    private javax.swing.JTextArea detailsText;
    private javax.swing.JList fileList;
    private javax.swing.JPanel filePanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;

	@Override
	public void displayConversationHistory(String path) {
		
		this.path = path;
		
		File dir = new File(this.path);
		ArrayList<String> conversations = new ArrayList<String>();
		
		if(dir.isDirectory()){
			String[] items = dir.list();
			for(int i = 0; i < items.length; i++){
				if(items[i].endsWith(".par")){
					conversations.add(items[i]);
				}
			}
			
			if(conversations.size() > 0){
				String[] names = new String [conversations.size()];
				conversations.toArray(names);
				final String[] content = names;
				fileList.setModel(new javax.swing.AbstractListModel() {
		            String[] strings = content;
		            public int getSize() { return strings.length; }
		            public Object getElementAt(int i) { return strings[i]; }
		        });
			}
		}
		
		
	}

	@Override
	public void setChatEngine(ChatEngine engine) {
		
		this.engine = engine;
		
	}

	@Override
	public void showWindow() {
		
		this.setVisible(true);
		
	}

}
